package org.mtmi.io.controls;

import java.io.IOException;

import net.n3.nanoxml.IXMLElement;

import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.mtmi.io.ReaderError;
import org.mtmi.io.SceneReader;
import org.mtmi.io.SceneWriter;
import org.mtmi.io.XMLReader;
import org.mtmi.io.XMLWriter;
import org.mtmi.ui.controls.AbstractRangeValueControl;
import org.mtmi.ui.controls.Control;
import org.mtmi.ui.controls.Slider;

/**
 * In addition to {@link AbstractRangeValueControl}, save {@link Slider} features:
 * <ul>
 * <li> the dot color.
 * <li> the knob color.
 * </ul>
 */
public class SliderIO extends RangeValueControlIO
{
	
	private static final String E_SLIDER_COLORS = "sliderColors"; //$NON-NLS-1$
	private static final String A_TC = "thumb"; //$NON-NLS-1$

	public SliderIO() {
		setProcessFeature( F_BACKGROUND_COLOR, true );
		setProcessFeature( F_FOREGROUND_COLOR, true );
	}
	
	@Override
	public void writeControl( Control c, XMLWriter w, SceneWriter sw ) throws IOException 
	{
		super.writeControl(c, w, sw);
		
		//colors
		Slider knob = (Slider)c;
		w.addElement( E_SLIDER_COLORS, A_TC, knob.getThumbColor() ); 
	}
	
	@Override
	public void readControl( Control c, IXMLElement element, XMLReader r, SceneReader sr ) throws ReaderError 
	{
		super.readControl(c, element, r, sr);
		
		//colors:
		IXMLElement e = r.getElement( element, E_SLIDER_COLORS, false );
		if( e!=null ) {
			RGB dc = r.getAttr( e, A_TC, (RGB)null ); 

			Slider slider = (Slider)c;
			slider.setThumbColor( new Color( c.getScene().getDisplay(), dc ) );
		}
	}
}
